<?php
include "../database/database.php";
class CouponBean
{
	public $couponid;
	public $coutitle;
	public $couimage;
	public $couprice;
	public $goodsid;
	public $isGet;
	public $shop;
	public $shopid;

	function __construct($couponId,$couTitle,$couImage,$couPrice,$goodsId,$isGet,$shop,$shopid)
	{
		$this->couponid = $couponId;
		$this->coutitle = $couTitle;
		$this->couimage = $couImage;
		$this->couprice = $couPrice;
		$this->goodsid = $goodsId;
		$this->isGet = $isGet;
		$this->shop = $shop;
		$this->shopid = $shopid;
	}
}

$enddate = date("Y-m-d");
$sql = "SELECT * FROM  coupon  where  enddate>='".$enddate."'   and coupon.state=1 ";
$result = $conn->query($sql);
$couPon = array();
if ($result->num_rows > 0 ) {
	while ($row = $result->fetch_assoc()) {
	     $num = $row['number'];
	     $shopid = $row['shopid'];
		if ($num != 0) {
			//判断用户是否领取优惠券
			$sql1="select * from mycoupon where userid='".$_REQUEST[userid]."' and couponid='".$row[couponid]."'";
			$result1=$conn->query($sql1);
			if ($result1->num_rows>0) {
				$isGet=1;//1为已经领取
			}else{
				$isGet=0;//0为未领取
			}
			//判断用户是否领取优惠券 end
			$shop=0;
			//获取该优惠券的商户名
			if($shopid!=0){
               $sql2="select * from shop where shopid=".$shopid;
				$result2=$conn->query($sql2);
				$row2 = $result2->fetch_assoc();
				if($row2){
				   $shop=$row2[name];	
				 }
			}
			$couPon[] = new CouponBean($row['couponid'],$row['title'],$row['images'],$row['price'],$row['goodsid'],$isGet,$shop,$shopid);
		}
	}
}

$conn->close();

echo json_encode($couPon);
?>